﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using BuXiaChu.OrderDishes.BLL.FileStore;
using BuXiaChu.OrderDishes.BLL.WebRuntime;

namespace BuXiaChu.OrderDishes.Web.Admin
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class Upload1 : HttpHandlerBase, IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            base.Init(context);
            AjaxActionResult result = new AjaxActionResult();
            try
            {
                IServiceProvider provider = ((IServiceProvider)context);
                HttpWorkerRequest worker = provider.GetService(typeof(HttpWorkerRequest)) as HttpWorkerRequest;
                byte[] buffer = new byte[0x2000];//缓冲8K
                //worker.re
                //context.Request.Files
                foreach (var file in context.Request.Files)
                {
                    var postFile = context.Request.Files[file.ToString()];
                    var content = new byte[postFile.ContentLength];
                    postFile.InputStream.Read(content, 0, postFile.ContentLength);

                    UploadFileEntity entity = new UploadFileEntity(111, postFile.FileName, content);
                    IFileUploadProvider upload = new ImageUploadProvider(entity, true, false);
                    upload.Save();
                }
                result.success = true;
                result.message = "上传成功";
            }
            catch (Exception ex)
            {
                result.message = "上传失败";
            }
            SerializeDataToResponseStream(result);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
